<?php
/*
 * @Descripttion: 
 * @version: 1.0
 * @Author: Sam
 * date: 2021-07-27 14:55:18
 * @LastEditors: Sam
 * @LastEditTime: 2023-02-10 12:20:51
 */

namespace app\admin\model;
use think\Model;


/**
 * 用户管理 model
 * Class User
 * @package app\admin\model\user
 */
class User extends BaseModel
{

    /**
     * 数据表主键
     * @var string
     */
    protected $pk = 'id';

    /**
     * 模型名称
     * @var string
     */
    protected $name = 'user';
    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = true;


        /**
     * 获取积分排行
     * @param [type] $value
     * @param [type] $data
     * @return void
     * @author Sam
     * @date 2022-12-21
     */
    public function getRankAttr($value,$data){
        $map_score_1 = ['score','>',$data['score']];
        $map_score_2 = [
            ['score','=',$data['score']],
            ['update_time','<',$data['update_time']],
        ];
        $map_score_3 = [
            ['score','=',$data['score']],
            ['update_time','=',$data['update_time']],
            ['id','<',$data['id']],
        ];
         $rank = self::where(function ($query) use ($map_score_1,$map_score_2,$map_score_3){
            $query->whereOr([$map_score_1,$map_score_2,$map_score_3]);
        })->count();
        return $rank + 1;
    }

    public function auth(){
        return $this->hasOne(UserAuth::class,'uid')->where('status',1);
    }
}